package top.jiangqiang.qyblog.common.exception;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import top.jiangqiang.qyblog.common.constant.ResponseEnum;

/**
 * 所有异常的基础类
 *
 * @author Jiangqiang
 * @version 1.0
 * @date 2023/3/14 8:53
 */
@Getter
public abstract class BaseException extends RuntimeException {
    protected final int code;
    protected final String message;

    public BaseException(String message, Object... arguments) {
        super(StrUtil.format(message, arguments));
        this.code = ResponseEnum.ERROR.getCode();
        this.message = StrUtil.format(message, arguments);
    }

    public BaseException(String message, Throwable cause) {
        super(message, cause);
        this.code = ResponseEnum.ERROR.getCode();
        this.message = message;
    }

    /**
     * code:500
     * message:系统内部错误
     */
    public BaseException(Throwable cause) {
        super(cause);
        this.code = ResponseEnum.INTERNAL_SERVER_ERROR.getCode();
        this.message = ResponseEnum.INTERNAL_SERVER_ERROR.getMessage();
    }

    public BaseException(ResponseEnum responseEnum) {
        super(responseEnum.getMessage());
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
    }
}
